/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity;

import java.util.HashMap;
import java.util.Map;
import share.PGHelper;
import share.PGMacro;

/**
 *
 * @author Salm
 */
public class Mail3 {
    private final Mail3.MailType type;

    private final long sendTime;
    private final String sender;
    private final String[] params;
    
    private transient Map<String, Object> amf = null;

    public Mail3(MailType type, String sender, long sendTime, String... params) {
        this.type = type;
        this.sendTime = sendTime;
        this.sender = sender;
        this.params = params;
    }
    
    public Map<String, Object> buildAMF(StringBuilder sb)
    {
        if (amf == null)
        {
            Map<String, Object> data = new HashMap(4);

            data.put(PGMacro.MAIL_ID, "");
            data.put(PGMacro.MAIL_SEND_TIME, sendTime);
            data.put(PGMacro.MAIL_SENDER, sender);
            data.put(PGMacro.MAIL_CONTENT, this.renderContent(sb));

            amf = data;
        }

        return amf;
    }
    
    private String renderContent(StringBuilder sb)
    {
        try
        {
            this.type.render(sb, this);
            return sb.toString();
        }
        catch (Exception ex)
        {
            return "Xin lỗi! Hệ thống không xử lý được mail này!";
        }
    }

    public static enum MailType
    {
        Text(0) {
            @Override
            public void render(StringBuilder sb, Mail3 mail) {
                if (mail.params.length > 0 &&
                    !PGHelper.isNullOrEmpty(mail.params[0]))
                {
                    sb.append(mail.params[0]);
                }
            }
        },
        HelpFish(1) {
            @Override
            public void render(StringBuilder sb, Mail3 mail) {
                sb.append("<text color=\"0xffffff\">");
                sb.append("<link type=\"user\" data=\"").append(mail.params[0]).append("\">");
                sb.append(mail.params[1]).append("</link>");
                sb.append("</text>");
                sb.append("<text> vừa giúp bạn ");
                sb.append(mail.params[2]).append(" cá </text>");
            }
        },
        StealEgg(2) {
            @Override
            public void render(StringBuilder sb, Mail3 mail) {
                sb.append("<text color=\"0xffffff\">");
                sb.append("<link type=\"user\" data=\"").append(mail.params[0]).append("\">");
                sb.append(mail.params[1]).append("</link>");
                sb.append("</text>");
                sb.append("<text> vừa trộm của bạn 1 trứng </text>");
                sb.append("<img src=\"").append(mail.params[2]).append("\" />");
            }
        },
        BuildSnowman(3) {
            @Override
            public void render(StringBuilder sb, Mail3 mail) {
                sb.append("<text color=\"0xffffff\">");
                sb.append("<link type=\"user\" data=\"").append(mail.params[0]).append("\">");
                sb.append(mail.params[1]).append("</link>");
                sb.append("</text>");
                sb.append("<text> vừa giúp bạn xây người tuyết!</text>");
            }
        };

        public abstract void render(StringBuilder sb, Mail3 mail);

        private final int typeID;
        private MailType(int typeID) {
            this.typeID = typeID;
        }

        public int getID() {
            return typeID;
        }

        public static MailType fromID(int typeID)
        {
            switch (typeID)
            {
                case 0:
                    return Text;
                case 1:
                    return HelpFish;
                case 2:
                    return StealEgg;
                case 3:
                    return BuildSnowman;
            }

            return Text;
        }
    }
}
